import { _decorator, Button, Label, Sprite } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import type { ServiceAPIURI, ServiceBody } from '../../api/api.config.service';
import GameAssets from '../../manager/game.assets';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

type INFO = mtec.OmitO<ServiceBody<ServiceAPIURI.Free>, 'userId'|'freeID'>;

@ccclass('FreeConfirm')
export class FreeConfirm extends BasePopUp<typeof Popup, boolean> {
	protected default_return: boolean;

	private lbl_name: Label;
	private lbl_account: Label;
	private spt_channel: Sprite;

	protected initAfterOnLoad(): void {
		this.lbl_name = this.node.getChildByName('lbl-name').getComponent(Label);
		this.lbl_account = this.node.getChildByName('lbl-account').getComponent(Label);
		this.spt_channel = this.node.getChildByName('icon-channel').getComponent(Sprite);

		this.renderLocalUI();
	}

	public onShow(info: INFO): void {
		this.lbl_name.string = info.accountName;
		this.lbl_account.string = info.account;

		mtec.cc.skinPeeler(this.spt_channel.node, this.smap.get(info.channel), {height: true});

		BasePopUp.jelly_enter(this.node);
	}

	protected _click_event_: { [name: string]: (this: FreeConfirm, button: Button) => void; } = {
		async close(btn){
			btn.interactable = false;
			await BasePopUp.jelly_leave(this.node);
			this.close(false);
			btn.interactable = true;
		},
		async confirm(btn){
			btn.interactable = false;
			await BasePopUp.jelly_leave(this.node);
			this.close(true);
			btn.interactable = true;
		}
	}

	private renderLocalUI(): void {
		(<const>[
			['head', GameAssets.getLangText(75)],
			['confirm', GameAssets.getLangText(76)]
		]).forEach(([name, text])=>{
			this.node.getChildByName('lbl-'+name).getComponent(Label).string = text;
		});
	}

}

